<template>
  <div class="wrapper">
    <div class="top" @click="goBack">
      <img src="@/assets/images/home.png" class="back" alt="header" />
    </div>
    <div class="main">
      <div class="header">
        <div class="header-title">
          <span class="title-text">新高考选科</span>
          <span class="title-desc">选择科目，获得精准专业、院校数据</span>
        </div>
      </div>
      <div class="form-box">
        <div class="form-item flex-center-center" @click="clickSelectProvince">
          <div class="form-item-title">高考省份</div>
          <div class="form-item-content">
            <div
              :class="[
                'form-item-content-item',
                province == '选择省份' ? 'disabled' : '',
              ]"
            >
              {{ province }}
            </div>
            <img
              src="@/assets/images/icon_arrow_next.png"
              class="arrow"
              alt=""
            />
          </div>
        </div>
        <div class="form-item" v-if="province != '北京'">
          <div class="form-item-title">首选科目</div>
          <div class="feature-box-feature">
            <div
              :class="{
                'province-item': true,
                active: item.checked,
              }"
              v-for="(item, index) in mainSubjects"
              :key="index"
              @click="onClickMainItem(item)"
            >
              {{ item.name }}
            </div>
          </div>
        </div>
        <div class="form-item" v-if="province != '北京'">
          <div class="form-item-title">再选科目</div>
          <div class="feature-box-feature">
            <div
              :class="{
                'province-item': true,
                active: item.checked,
              }"
              v-for="(item, index) in minorSubjects"
              :key="index"
              @click="onClickMinorItem(item)"
            >
              {{ item.name }}
            </div>
          </div>
        </div>
        <div class="form-item" v-if="province == '北京'">
          <div class="feature-box-feature">
            <div
              :class="{
                'province-item': true,
                active: item.checked,
              }"
              v-for="(item, index) in sixSubjects"
              :key="index"
              @click="onClickSixItem(item)"
            >
              {{ item.name }}
            </div>
          </div>
        </div>
        <div class="btn-box">
          <div class="btn" @click="handleClickCheck">立即查询</div>
          <div :class="['btn', 'spe']" @click="handleClickCompare">
            加入对比
          </div>
        </div>
      </div>
      <div class="tip">*参考2025高考招生情况</div>
    </div>

    <ProvincePopup
      v-if="isShowProvincePopup"
      :provinces="provinces"
      :selectProvinceObj="selectProvinceObj"
      @onProvinceSelect="onProvinceSelect"
      @onCloseProvince="onCloseProvince"
    >
    </ProvincePopup>
  </div>
</template>

<script setup>
import { ref, onMounted, computed, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { cloneDeep } from 'lodash-es';
import { onBackPressed } from '@/utils/androidFn';
import { useSubjectSelectStore } from '@/store/subjectSelect';
import { getVolunteerRegion, recodeAddOne } from '@/api/volunteer';
import {
  mainSubjectList,
  minorSubjectList,
  sixSubjectList,
} from '@/utils/subjectData';
import ProvincePopup from '@/components/ProvincePopup.vue';
import { showToast } from 'vant';

const router = useRouter();
const store = useSubjectSelectStore();

const mainSubjects = ref(cloneDeep(mainSubjectList));
const minorSubjects = ref(cloneDeep(minorSubjectList));
const sixSubjects = ref(cloneDeep(sixSubjectList));
const onClickMainItem = (item, index) => {
  mainSubjects.value.forEach((item) => {
    item.checked = false;
  });
  item.checked = !item.checked;
};

const onClickMinorItem = (item, index) => {
  // 如果当前项未选中，且已经有2个选中项，则不允许再选
  const selectedItems = minorSubjects.value.filter((item) => item.checked);
  if (!item.checked && selectedItems.length >= 2) {
    showToast('最多只能选择2个科目');
    return;
  }

  // 切换当前项的选中状态
  item.checked = !item.checked;
};
const onClickSixItem = (item, index) => {
  // 如果当前项未选中，且已经有2个选中项，则不允许再选
  const selectedItems = sixSubjects.value.filter((item) => item.checked);
  if (!item.checked && selectedItems.length >= 3) {
    showToast('最多只能选择3个科目');
    return;
  }
  // 切换当前项的选中状态
  item.checked = !item.checked;
};
const provinces = ref([]);
const getVolunteerRegionFn = async () => {
  const params = {
    platform: 4,
  };
  const res = await getVolunteerRegion(params);
  if (res.code == 200) {
    provinces.value = [];
    res.data.forEach((item) => {
      if (item.type != 2) {
        provinces.value.push({
          ...item,
          name: item.regionName,
          value: item.regionId,
          checked: false,
        });
      }
    });
    store.setProvinces(provinces.value);
  }
};
getVolunteerRegionFn();

const province = ref('选择省份');
const isShowProvincePopup = ref(false);
const selectProvinceObj = ref({});

const onProvinceSelect = (item) => {
  isShowProvincePopup.value = false;
  selectProvinceObj.value = item;
  province.value = item.name;
};

const onCloseProvince = () => {
  isShowProvincePopup.value = false;
};

const clickSelectProvince = () => {
  isShowProvincePopup.value = true;
};

const handleClickCheck = () => {
  if (!selectProvinceObj.value.name) {
    showToast('请选择省份');
    return;
  }
  if (province.value != '北京') {
    const selectMainSubject = mainSubjects.value.filter((item) => item.checked);
    const selectMinorSubject = minorSubjects.value.filter(
      (item) => item.checked
    );
    if (selectMainSubject.length === 0) {
      showToast('请选择首选科目');
      return;
    }
    if (selectMinorSubject.length < 2) {
      showToast('请选择两个再选科目');
      return;
    }
    const temp = [...selectMainSubject, ...selectMinorSubject];
    const cacheData = {
      subjectList: temp,
      province: selectProvinceObj.value,
    };
    store.setHomeData(cacheData);
    recodeAddOneFn(cacheData);
    router.push({
      path: '/subjectReport',
    });
  } else {
    const selectSixSubject = sixSubjects.value.filter((item) => item.checked);
    if (selectSixSubject.length < 3) {
      showToast('请选择三个科目');
      return;
    }
    const cacheData = {
      subjectList: selectSixSubject,
      province: selectProvinceObj.value,
    };
    store.setHomeData(cacheData);
    recodeAddOneFn(cacheData);
    router.push({
      path: '/subjectReport',
    });
  }
};
const handleClickCompare = () => {
  if (!selectProvinceObj.value.name) {
    showToast('请选择省份');
    return;
  }
  if (province.value != '北京') {
    const selectMainSubject = mainSubjects.value.filter((item) => item.checked);
    const selectMinorSubject = minorSubjects.value.filter(
      (item) => item.checked
    );
    if (selectMainSubject.length === 0) {
      showToast('请选择首选科目');
      return;
    }
    if (selectMinorSubject.length < 2) {
      showToast('请选择两个再选科目');
      return;
    }
    const temp = [...selectMainSubject, ...selectMinorSubject];
    const cacheData = {
      subjectList: temp,
      province: selectProvinceObj.value,
    };
    store.setHomeData(cacheData);
    store.setCompareData(cacheData);
    recodeAddOneFn(cacheData);
    router.push({
      path: '/subjectCompare',
    });
  } else {
    const selectSixSubject = sixSubjects.value.filter((item) => item.checked);
    if (selectSixSubject.length < 3) {
      showToast('请选择三个科目');
      return;
    }
    const cacheData = {
      subjectList: selectSixSubject,
      province: selectProvinceObj.value,
    };
    store.setHomeData(cacheData);
    store.setCompareData(cacheData);
    recodeAddOneFn(cacheData);
    router.push({
      path: '/subjectCompare',
    });
  }
  // router.push({
  //   path: '/subjectCompare',
  // });
};

const recodeAddOneFn = async (content) => {
  const params = {
    recordType: 30, // 保存立即对比的参数
    searchContent: JSON.stringify(content),
  };
  const res = await recodeAddOne(params);
};

const goBack = () => {
  onBackPressed(router);
};

const getCacheData = () => {
  selectProvinceObj.value = store.homeData.province;
  province.value = selectProvinceObj.value.name || '选择省份';
  const temp = store.homeData.subjectList;
  if (temp.length) {
    temp.forEach((item) => {
      mainSubjects.value.forEach((_item) => {
        if (_item.name == item.name) {
          _item.checked = true;
        }
      });
      minorSubjects.value.forEach((_item) => {
        if (_item.name == item.name) {
          _item.checked = true;
        }
      });
      sixSubjects.value.forEach((_item) => {
        if (_item.name == item.name) {
          _item.checked = true;
        }
      });
    });
  }
};

getCacheData();
</script>

<style scoped lang="scss">
.wrapper {
  width: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: url('@/assets/images/homeBg.png') no-repeat center center;
  background-size: 100% 100%;
  .top {
    position: absolute;
    padding: 28px 30px 28px 30px;
    .back {
      width: 40px;
      height: 40px;
    }
  }
  .main {
    height: calc(100vh - 120px);
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 36vw;
    margin: 0 auto;
    .header {
      display: flex;
      .robot-icon {
        width: 136px;
        height: 120px;
        margin-right: 18px;
      }
      .header-title {
        display: flex;
        flex-direction: column;
        .title-text {
          font-weight: bold;
          font-size: 44px;
          color: #000000;
          margin-bottom: 20px;
          white-space: nowrap;
        }
        .title-desc {
          font-weight: 400;
          font-size: 24px;
          color: #555555;
          white-space: nowrap;
        }
      }
    }
    .form-box {
      margin-top: 52px;
      width: 100%;
      box-sizing: border-box;
      padding: 48px 32px;
      position: relative;
      border-radius: 32px;
      background-color: #f6fbfe;
      &::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        border-radius: 32px;
        padding: 2px;
        background: linear-gradient(
          180deg,
          rgba(255, 255, 255, 1),
          rgba(206, 231, 245, 1)
        );
        -webkit-mask: linear-gradient(#fff 0 0) content-box,
          linear-gradient(#fff 0 0);
        mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
        -webkit-mask-composite: xor;
        mask-composite: exclude;
        pointer-events: none;
      }
      .form-item {
        position: relative;
        z-index: 1;
        justify-content: space-between;
        padding: 24px 0;
        border-bottom: 1px solid $border-color;
        &:first-child {
          padding-top: 0;
        }
        .form-item-title {
          display: flex;
          align-items: center;
          font-size: 26px;
          color: $title-color;
          font-weight: bold;
        }
        .form-item-content {
          display: flex;
          align-items: center;
          font-size: 26px;
          color: $title-color;
          font-weight: bold;
          .form-item-content-item {
            padding: 0;
            margin-right: 12px;
            &.disabled {
              color: #999;
            }
          }
          .arrow {
            width: 30px;
            height: 30px;
          }
          :deep(.van-field) {
            background-color: #f6fbfe;
            .van-field__control {
              font-size: 26px;
              color: #000000;
            }
          }
        }
        .feature-box-feature {
          display: flex;
          flex-wrap: wrap;
          gap: 14px;
          margin-top: 24px;
          .province-item {
            flex-shrink: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 8px;
            font-family: 'PingFang-SC, PingFang-SC';
            font-size: 26px;
            padding: 8px 28px;
            color: #333333;
            background: #fff;
            border: #fff 1px solid;
            &.active {
              color: $primary-color;
              border: $primary-color 1px solid;
              background: #e5edff;
            }
          }
        }
      }
      .btn-box {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .btn {
          width: 48%;
          text-align: center;
          background: $primary-color;
          border-radius: 12px;
          font-weight: bold;
          font-size: 26px;
          color: #ffffff;
          margin-top: 40px;
          padding: 16px 0;
        }
        .spe {
          position: relative;
          background: linear-gradient(240deg, #597dff 0%, #9d19ff 100%);
        }
        .new-icon {
          position: absolute;
          top: -18px;
          right: 14px;
          width: 72px;
          height: 40px;
        }
      }
    }
    .tip {
      font-size: 22px;
      color: #888;
      margin-top: 24px;
      text-align: right;
    }
  }
}
</style>
